<!-- 显示数据的列表 -->
<template>
  <div class="system-information-list-data-list">
    <el-table ref="multipleTable" :data="dataTable" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" @select-all="selectAll">
      <el-table-column type="selection" width="55" fixed="left">
      </el-table-column>
      <!-- <el-table-column label="排序" prop="orderNum" width="90">
        <template slot-scope="scope">
          <div class="order-num" v-loading="isLoading[scope.row.id]">
            <el-input v-model="scope.row.orderNum" size="small" style="width:70px;" maxlength="7"
              @blur="blurOrderNum(scope.row.id,scope.row.orderNum)"></el-input>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="图片" width="88" align="center">
        <template slot-scope="scope">
          <div class="dfjcac">
            <img style="max-height:40px;max-width:80px;" :src="scope.row.imageList && scope.row.imageList.length > 0 && scope.row.imageList[0].src ? scope.row.imageList[0].src: ''" />
          </div>
        </template>
      </el-table-column>
      <el-table-column v-for="titleItem in customeTitle" :key="titleItem.informationTitleId" :label="titleItem.title" min-width="120" class-name="cusxx">
        <template slot-scope="scope">
          <div v-for="rowtitem in titleItemFilter(scope.row.titleItem,titleItem.informationTitleId)" :key="rowtitem.pdtableTitleId">
            <TooltipOver :tipValue="rowtitem.value" :refTipName="'ref-'+ rowtitem.informationTitleId + '-' + scope.row.id" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="文件" width="120" class-name="cusxx">
        <template slot-scope="scope">
          <TooltipOver :tipValue="scope.row.file && scope.row.file.originalFilename ? scope.row.file.originalFilename : ''" :refTipName="'ref-file-' + scope.row.id" v-if="scope.row.file && scope.row.file.originalFilename"/>
        </template>
      </el-table-column>
      <el-table-column label="显示否" prop="isShow" width="100" fixed="right">
        <template slot-scope="scope">
          <el-switch v-model="isShow[scope.row.id]" active-color="#13ce66"
            @change="changeShow(scope.row.id, isShow[scope.row.id])">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip width="100" fixed="right">
        <template slot-scope="scope">
          <!-- <el-button v-if="scope.row.isTop > 0" @click="checkTopItem(scope.row.id)" type="text"><span
              class="orange">取消置顶</span></el-button>
          <el-button v-if="scope.row.isTop < 1" @click="checkTopItem(scope.row.id)" type="text"><span
              class="green">置顶</span></el-button> -->
          <el-button @click="eidtItem(scope.row.id)" type="text">编辑</el-button>
          <el-button @click="deleteItem(scope.row.id)" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import TooltipOver from '@/components/common/TooltipOver.vue'
import { getSystemItemPathBySystemAimId } from '@/core/functions.js'
export default {
  props: {
    dataTable: {
      type: Array
    },
    data: {
      type: Object
    },
    multipleSelection: {
      type: Array
    },
    customeTitle: {
      type: Array
    },
    multipleSelectionChange: {
      type: Function
    },
    resetData: {
      type: Function
    },
    system: {
      type: Object
    },
    setDataTable: {
      type: Function
    },
    page: {
      type: Object
    }
  },
  data () {
    return {
      isShow: [],
      isLoading: {}
    }
  },
  watch: {
    dataTable: {
      handler (newValue) {
        if (newValue.length > 0) {
          newValue.forEach(item => {
            this.isShow[item.id] = item.status > 0
            this.isLoading[item.id] = false
          })
        } else {
          this.isShow = []
        }
      },
      deep: true
    }
  },
  filters: {
  },
  methods: {
    // 批量删除选中回调方法
    handleSelectionChange (val) {
      this.multipleSelectionChange(val)
    },
    selectAll (val) {
      if (!val.length && !this.dataTable.length) {
        this.$refs.multipleTable.clearSelection()
      }
    },
    // 过滤
    titleItemFilter (titleItem, titleId) {
      return titleItem.filter((item) => {
        return item.informationTitleId === titleId
      })
    },
    checkTopItem (id) {
      let item = this.dataTable.find((element) => {
        return element.id === id
      })
      if (!item) {
        return
      }
      let param = {
        id: id,
        isTop: item.isTop < 1 ? 1 : 0
      }
      this.$post('/system/download/info-change-istop', param).then(response => {
        if (response && response > 0) {
          this.resetData()
        }
      })
    },
    changeShow (id, isShow) {
      let param = {
        id: id,
        status: isShow ? 1 : 0
      }
      // 请求后台修改状态
      this.$post('/system/information/data-update-status', param).then(response => {
        if (response && response > 0) {
          let show = this.isShow
          show[id] = isShow
          this.isShow = JSON.parse(JSON.stringify(show))
        }
      })
    },
    // 编辑
    eidtItem (id) {
      let item = this.dataTable.find((element) => {
        return element.id === id
      })
      if (!item) {
        return
      }
      let fullPath = getSystemItemPathBySystemAimId(this.system, item.id)
      if (!fullPath) {
        return
      }
      // 存储列表页码
      sessionStorage.setItem('systbPageCur' + this.system.id, JSON.stringify({pageCur: this.page.page, fromItem: false}))
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    // 删除
    deleteItem (id) {
      let item = this.dataTable.find((element) => {
        return element.id === id
      })
      if (!item) {
        return
      }

      this.$confirm('确认要删除 ' + item.titleItem[0].value + ' 信息项？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.$post('/system/information/data-delete', {
            id: id
          }).then((response) => {
            if (response && response.length > 0) {
              this.resetData()
            }
          })
        })
        .catch(() => {
        })
    },
    // 排序号改变
    blurOrderNum (id, val) {
      let item = this.data.find(item => {
        return parseInt(item.id) === parseInt(id)
      })
      if (!item) {
        return
      }
      if (parseInt(item.orderNum) === parseInt(val)) {
        return
      }
      var re = /^[0-9]+$/
      if (!re.test(val)) {
        this.$message.error('序号不能为空，且只能输入数字！')
        let dataTable = JSON.parse(JSON.stringify(this.dataTable))
        for (let i in dataTable) {
          if (parseInt(dataTable[i].id) === parseInt(id)) {
            dataTable[i].orderNum = item.orderNum
          }
        }
        this.setDataTable(dataTable)
        return
      }
      let param = { id: id, orderNum: val }
      let isLoading = JSON.parse(JSON.stringify(this.isLoading))
      isLoading[id] = true
      this.isLoading = isLoading
      // 请求修改排序
      this.$post('/system/download/info-change-ordernum', param).then(response => {
        if (response && parseInt(response) > 0) {
          this.resetData()
        }
      })
    }
  },
  components: {
    TooltipOver
  }
}

</script>

<style scoped lang="scss">
.system-information-list-data-list /deep/ {
  .el-table th {
    color: #555;
  }
  .el-table-column--selection .cell {
    padding-left: 10px;
  }
}
.system-information-list-data-list .order-num .el-input__inner {
  padding: 0 8px;
}

</style>
